<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<bydFileImport :apiObj="$api.filing.uploadFilingSeal" :templateUrl="templateUrl" @success="success">
					<template #default="{open}">
						<el-button type="primary" plain icon="el-icon-FolderAdd" @click="open">私印备案</el-button>
					</template>
				</bydFileImport>
				<el-button type="primary" plain icon="el-icon-Sort" @click="sealForwardAction">印章转交</el-button>
				<el-button type="danger" plain icon="el-icon-delete" @click="batch_del">导出Excel</el-button>
			</div>
			
		</el-header>
		<el-main class="nopadding">
			<bydTable ref="table" :apiObj="apiObj" row-key="tfId" @selection-change="selectionChange" stripe border>
				<el-table-column type="selection" fixed width="60" reserve-selection></el-table-column>
				<el-table-column label="序号" fixed type="index" width="50"></el-table-column>
				<!-- <el-table-column label="系统序号" prop="sealId" width="50"></el-table-column> -->
				<el-table-column label="地区" prop="place" width="150"></el-table-column>
				<el-table-column label="工厂" prop="factory" min-width="200"></el-table-column>
				<el-table-column label="部门"  prop="department" width="200"></el-table-column>
				<el-table-column label="科室"  prop="office" width="200"></el-table-column>
				<el-table-column label="监印人" prop="keepName" width="150"></el-table-column>
				<el-table-column label="印章名称" prop="sealName" show-overflow-tooltip width="200"></el-table-column>
				<el-table-column label="物料编码" prop="materialCode" width="150"></el-table-column>
				<el-table-column label="规格" prop="specifications" width="150"></el-table-column>
				<el-table-column label="印模颜色" prop="sealColor" width="100"></el-table-column>
				<el-table-column label="数量" prop="sealCount" width="100"></el-table-column>
				<el-table-column label="用途" prop="effect" width="300"></el-table-column>
				<el-table-column label="刻制日期" prop="engraveDate" width="150"></el-table-column>
				<el-table-column label="刻制人" prop="engraveName" width="150"></el-table-column>
				<el-table-column label="备案人" prop="filingName" width="150"></el-table-column>
				<el-table-column label="备注" prop="remark" width="200"></el-table-column>
				<el-table-column label="状态" prop="state" width="100"></el-table-column>
				<el-table-column label="销毁日期" prop="destroyDate" width="150"></el-table-column>
				<el-table-column label="销毁原因" prop="destroyReason" width="200"></el-table-column>
				<el-table-column label="销毁人" prop="destroyPeople" width="150"></el-table-column>
				<el-table-column label="印章图样" prop="sealPattern" width="150" fixed="right">
					<template #default="scope">
                        <bydWaterMark ref="wm" text="印章管理系统" subtext="印章图样">
                           <el-image @click="handleLoad"
                                style="width: 120px; height: 100px" 
                                :preview-teleported="true" 
                                :src="scope.row.sealPattern" 
                                :fit="fit" 
                                :preview-src-list="[scope.row.sealPattern]">
                            </el-image>
                        </bydWaterMark>
                    </template>
				</el-table-column>
				<el-table-column label="操作"  align="center" width="120" fixed="right">
					<template #default="scope">
						<el-button type="text" size="small" @click="processShow(scope.row.trackId)">遗失</el-button>
					</template>
				</el-table-column>
			</bydTable>
		</el-main>
	</el-container>
	<el-drawer v-model="sealForwardView" :size="'60%'" title="印章转交" custom-class="drawerBG" direction="rtl" destroy-on-close>
		<sealForward :checkedSeal="selection"></sealForward>
	</el-drawer>
</template>

<script>
	import { getServerUrl } from '@/utils'
	import { create } from '@/utils/watermark'
	import bydFileImport from '@/components/bydFileImport'
	import sealForward from '@/views/divisionSeal/entitySeal/sealForward.vue'
	export default {
		name: "entitySeal",
		components:{create,bydFileImport,sealForward},
		data(){
			const templateUrl = getServerUrl('/login/download?fileId=1');
			return {
				apiObj: this.$api.filing.entitySealList,
				selection: [],
				sealForwardView : false,
				templateUrl
			}
		},
		methods: {
			//查看
			processShow(trackId){
				console.log(trackId)
			},
			//添加水印
			handleLoad() {
                setTimeout(() => {
                    const img = document.querySelector('.el-image-viewer__canvas');
                    var userInfo = this.$tool.data.get("USER_INFO");
                    create(img, { text: '印章管理系统',subtext : userInfo.username + userInfo.jobnumber })
                }, 100)
            },
			//未经过刻制流程直接备案
			success(res, close){
				if(res.status==200){
					this.$refs.table.refresh();
					this.$alert("点击确定按钮结束上传。", "导入成功", {
						type: "success",
						showClose: false,
						center: true,
						confirmButtonText: '确定',
					})
					close()
				}else{
					//返回失败后的自定义操作
					this.$message.error(res.message);
				}
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
				console.log(selection)
			},
			sealForwardAction(){
				const userInfo = this.$tool.data.get("USER_INFO");
				if(this.selection.length == 0){
					this.$message({
						type : 'warning',
						message : '请选择需要转发的印章'
					})
					return false;
				}
				//只有在用的印章才可以进项转交
				var notUseSeal = this.selection.every((item,id) => {
					return item.state == "在用"
				})
				if(!notUseSeal){
					this.$message.error("选择的印章中包含转交中的印章，无法进行转交");
					return false;
				}
				//普通用户只可以转交自己的印章
				if(userInfo.salt == "4"){
					const loginUser = userInfo.username + userInfo.jobnumber
					var finish = this.selection.every((item,id) => {
						return item.keepName == loginUser
					})
					if(!finish){
						this.$message.error("您选择的印章监印人必须是" + loginUser);
						return false;
					}
				}
				//所选印章需属于相同的部门
				const factoryInfo = this.selection[0].place + this.selection[0].sealFactory;
				var oneDepartment = this.selection.every((item,id) => {
					const itemFactoryInfo = item.place + item.sealFactory;
					return itemFactoryInfo == factoryInfo
				})
				if(!oneDepartment){
					this.$message.error("您选择的印章监印人必须隶属同一地区和工厂");
					return false;
				}
				this.sealForwardView = true;
			}
		}
	}
</script>
<style>
</style>
